package com.huaxin.hxmoduleworkorder.mapping;

import com.huaxin.hxmoduleworkorder.common.enums.InspectionOrderPublishType;
import com.huaxin.hxmoduleworkorder.common.enums.InspectionOrderStatus;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionOrderBatchCreateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionOrderCreateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionOrderUpdateReqDTO;
import com.huaxin.hxmoduleworkorder.models.InspectionOrder;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

import java.util.List;

@Mapper(componentModel = "spring")
public interface InspectionOrderMapping {
    @Mapping(target = "publishType", ignore = true)
    @Mapping(target = "status", ignore = true)
    @Mapping(target = "customers", ignore = true)
    InspectionOrder toEntity(InspectionOrderCreateReqDTO in);

    @AfterMapping
    default void afterMapping(InspectionOrderCreateReqDTO dto, @MappingTarget InspectionOrder entity) {
        entity.setStatus(InspectionOrderStatus.PUBLISHED);
        entity.setPublishType(InspectionOrderPublishType.MANUAL);
    }

    @Mapping(target = "publishType", ignore = true)
    @Mapping(target = "status", ignore = true)
    List<InspectionOrder> toEntityList(List<InspectionOrderCreateReqDTO> in);

    void updateEntity(InspectionOrderUpdateReqDTO in, @MappingTarget InspectionOrder entity);

    @Mapping(target = "publishType", ignore = true)
    @Mapping(target = "status", ignore = true)
    InspectionOrder toAutoOrderEntity(InspectionOrderBatchCreateReqDTO in);

    @AfterMapping
    default void afterMapping(InspectionOrderBatchCreateReqDTO in, @MappingTarget InspectionOrder entity) {
        entity.setStatus(InspectionOrderStatus.PUBLISHED);
        entity.setPublishType(InspectionOrderPublishType.AUTO);
    }
}